﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Noria.Web.MyMSDN
{
    internal static class MoreLinq
    {
        /// <summary>
        /// http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property
        /// </summary>
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>
            (this IEnumerable<TSource> source, Func<TSource, TKey> selector)
        {
            var seenKeys = new HashSet<TKey>();
            return source.Where(element => seenKeys.Add(selector(element)));
        }
    }
}